﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    // NOTE: If you change the class name "Employee" here, you must also update the reference to "Employee" in App.config.
    public partial class MainService : ILeave
    {
        public int Leave_Using(int LeaveID)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Leave_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@LeaveID", LeaveID);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet Leave_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Leave_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Leave_GetByBranchCode(string _BranchCode)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_LEAVE_GETByBranchCode", dbConn);
            dbCmd.Parameters.AddWithValue("@Leave_BranchCode", _BranchCode);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public DataSet Leave_GetList(out int ItemCount, int currentPage, int pageSize, string _BranchCode)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Leave_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@PageSize", pageSize);
            dbCmd.Parameters.AddWithValue("@BranchCode", _BranchCode);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public LeaveInfo Leave_GetInfo(int LeaveID)
        {
            LeaveInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Leave_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Leave_ID", LeaveID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new LeaveInfo();
                    retVal.ID = Convert.ToInt32(dr["Leave_ID"]);
                    retVal.Code = dr["Leave_Code"].ToString();
                    retVal.Name = dr["Leave_Name"].ToString();
                    retVal.Status = Convert.ToBoolean(dr["Leave_Status"].ToString());
                    retVal.DateNumber = Convert.ToInt32(dr["Leave_DateNumber"]);
                    retVal.SalaryFactory = Convert.ToDecimal(dr["Leave_SalaryFactory"]);
                    retVal.SocialInsurance = Convert.ToBoolean(dr["Leave_SocialInsurance"]);
                    retVal.MedicalInsurance = Convert.ToBoolean(dr["Leave_MedicalInsurance"]);
                    retVal.BranchCode = dr["Leave_BranchCode"].ToString();
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int Leave_Insert(LeaveInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Leave_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Leave_Code", eInfo.Code);
            dbCmd.Parameters.AddWithValue("@Leave_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Leave_Status", eInfo.Status);
            dbCmd.Parameters.AddWithValue("@Leave_DateNumber", eInfo.DateNumber);
            dbCmd.Parameters.AddWithValue("@Leave_SalaryFactory", eInfo.SalaryFactory);
            dbCmd.Parameters.AddWithValue("@Leave_SocialInsurance", eInfo.SocialInsurance);
            dbCmd.Parameters.AddWithValue("@Leave_MedicalInsurance", eInfo.MedicalInsurance);
            dbCmd.Parameters.AddWithValue("@Leave_BranchCode", eInfo.BranchCode);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Leave_Update(LeaveInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Leave_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Leave_ID", eInfo.ID);
            dbCmd.Parameters.AddWithValue("@Leave_Code", eInfo.Code);
            dbCmd.Parameters.AddWithValue("@Leave_Name", eInfo.Name);
            dbCmd.Parameters.AddWithValue("@Leave_Status", eInfo.Status);
            dbCmd.Parameters.AddWithValue("@Leave_DateNumber", eInfo.DateNumber);
            dbCmd.Parameters.AddWithValue("@Leave_SalaryFactory", eInfo.SalaryFactory);
            dbCmd.Parameters.AddWithValue("@Leave_SocialInsurance", eInfo.SocialInsurance);
            dbCmd.Parameters.AddWithValue("@Leave_MedicalInsurance", eInfo.MedicalInsurance);
            dbCmd.Parameters.AddWithValue("@Leave_BranchCode", eInfo.BranchCode);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool Leave_Delete(int LeaveID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Leave_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Leave_ID", LeaveID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

